package com.java.controller.admin;


import com.java.exceptioin.SrbException;
import com.java.pojo.entity.Dict;
import com.java.result.Result;
import com.java.result.ResultCodeEnum;
import com.java.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */
@Api("数据字典")
@RestController
@RequestMapping("admin/core/dict")
public class AdminDictController {

    @Autowired
    private DictService dictService;


    //数据字典
    @ApiOperation("数据字典")
    @GetMapping("/getDicts/{id}")
    public Result getDicts(@PathVariable Long id){
        List<Dict> list =  dictService.getDicts(id);
        return Result.ok(list);
    }


    //导出
    @ApiOperation("数据字典导出")
    @GetMapping("excel/export")
    public Result export(HttpServletResponse response){
        dictService.export(response);
        return Result.ok().message("数据导出成功");
    }

    //导入
    @ApiOperation("数据字典导入")
    @PostMapping("excel/import")
    public Result importExcel(@RequestParam("file") MultipartFile file){
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            dictService.importExcel(inputStream);
            return Result.ok().message("数据导入成功");
        } catch (IOException e) {
            throw  new SrbException(ResultCodeEnum.DATA_ERROR);
        }
    }
}

